package server.core;

import server.*;
import server.valves.ErrorDispatcherValve;
import server.valves.ErrorReportValve;

public class StandardHost extends ContainerBase implements Host {
    private static final String info = "server.core.StandardHost/1.0";
    public StandardHost() {
        pipeline.setBasic(new StandardHostValve());
    }

    //实现Container接口------------------------------------------------------------------

    @Override
    public void addChild(Container child) {
        if (!(child instanceof Context))
            throw new IllegalArgumentException("host容器的子容器必须是Context容器");
        super.addChild(child);
    }

    @Override
    public String getInfo() {
        return info;
    }
    //实现Host接口------------------------------------------------------------------

    @Override
    public Context map(String uri) {
        if (uri == null)
            return null;
        Context context = null;
        int index = uri.indexOf('/', 1);
        if (index >= 0)
            uri = uri.substring(1, index);
        else
            uri = uri.substring(1);
        context = (Context) findChild(uri);

        //找默认的应用容器
//        if (context == null)
//            context = (Context)findChild("");
        return context;
    }

    //实现Lifecycle接口------------------------------------------------------------------

    @Override
    public synchronized void start() throws LifecycleException {
        addValve(new ErrorReportValve());
        addValve(new ErrorDispatcherValve());
        Mapper mapper = new StandardHostMapper();
        mapper.setProtocol("http");
        addMapper(mapper);
        super.start();
    }
}
